package com.hdjd.service.imp;

import com.hdjd.dao.GoodsDao;
import com.hdjd.dao.GoodsImgDao;
import com.hdjd.dao.ShopCarDao;
import com.hdjd.entities.Goods;
import com.hdjd.entities.ShopCar;
import com.hdjd.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * @author 黄徵岚
 * @create 2021/1/28 10:35
 */
@Service
public class ShopCartServiceImp implements ShopCartService {

    @Autowired
    private ShopCarDao shopCarDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsImgDao goodsImgDao;
    @Override
    public boolean addOrUpdateShopCartByAccIdAndGoId(Integer goId, Integer accId) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        String currentTime = sdf.format(calendar.getTime());
        Integer shopCartId = this.shopCarDao.selectShopCarIdByAccIdAndGoId(goId,accId);
        Goods goods = this.goodsDao.selectGoodsByGoId(goId);
        ShopCar shopCar = new ShopCar();
        shopCar.setGoNum(1);
        shopCar.setTotalPri(goods.getGoPri());
        shopCar.setCommitTime(currentTime);
        int operationInfo;
        if (shopCartId != null){
            shopCar.setShopCarId(shopCartId);
            operationInfo = this.shopCarDao.updateByPrimaryKeySelective(shopCar);
        }else {
            shopCar.setAccId(accId);
            shopCar.setGoId(goId);
            shopCar.setGoPri(goods.getGoPri());
            shopCar.setGoNm(goods.getGoNm());
            operationInfo = this.shopCarDao.insert(shopCar);
        }
        if (operationInfo != 0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public int editShopCartBuyNumByShopCartId(Integer shopCarId,Integer goNum) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        String currentTime = sdf.format(calendar.getTime());
        ShopCar shopCar = this.shopCarDao.selectByPrimaryKey(shopCarId);
        shopCar.setGoNum(goNum);
        shopCar.setTotalPri(shopCar.getGoPri()*goNum);
        shopCar.setCommitTime(currentTime);
        return this.shopCarDao.updateByPrimaryKey(shopCar);
    }

    @Override
    public int deleteShopCartByShopCartId(Integer shopCarId) {
        return this.shopCarDao.deleteByPrimaryKey(shopCarId);
    }

    @Override
    public int deleteShopCartByShopCartIdString(String shopCarIdString) {
        String[] shopCarIds = shopCarIdString.split(",");
        int i = 0;
        for (; i < shopCarIds.length; i++) {
            Integer cartId = Integer.parseInt(shopCarIds[i]);
            this.shopCarDao.deleteByPrimaryKey(cartId);
        }
        if (i != shopCarIds.length){
            return 0;
        }else {
            return i;
        }
    }

    @Override
    public List<ShopCar> selectShopCartByAccId(Integer accId) {
        List<ShopCar> shopCars = this.shopCarDao.selectShopCartByAccId(accId);
        for (ShopCar shopCar : shopCars) {
            shopCar.setImgUrl(this.goodsDao.selectGoodsByGoId(shopCar.getGoId()).getImgUrl());
            shopCar.setGoDp(this.goodsDao.selectGoodsByGoId(shopCar.getGoId()).getGoDp());
        }
        return shopCars;
    }
}
